﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
		<title>Startup Configuration</title>
		<link type="text/css" rel="stylesheet" href="bootstrap.min.css" />
	</head>

	<body>

		<div class="document-contents">


			<p>ASP.NET Boilerplate provides an infrastructure and a model to configure it and
				<a href="/Pages/Documents/Module-System">modules</a> on startup.</p>

			<h3 id="DocConfigureAbp">Configuring ASP.NET Boilerplate</h3>
			<p>Configuring ASP.NET Boilerplate is made on <strong>PreInitialize</strong> event of your 
module. Example configuration:</p>
			<pre lang="cs">public class SimpleTaskSystemModule : AbpModule
{
    public override void PreInitialize()
    {
        //Add languages for your application
        Configuration.Localization.Languages.Add(new LanguageInfo(&quot;en&quot;, &quot;English&quot;, &quot;famfamfam-flag-england&quot;, true));
        Configuration.Localization.Languages.Add(new LanguageInfo(&quot;tr&quot;, &quot;T&#252;rk&#231;e&quot;, &quot;famfamfam-flag-tr&quot;));

        //Add a localization source
        Configuration.Localization.Sources.Add(
            new XmlLocalizationSource(
                &quot;SimpleTaskSystem&quot;,
                HttpContext.Current.Server.MapPath(&quot;~/Localization/SimpleTaskSystem&quot;)
                )
            );

        //Configure navigation/menu
        Configuration.Navigation.Providers.Add&lt;SimpleTaskSystemNavigationProvider&gt;();        
    }

    public override void Initialize()
    {
        IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
    }
}</pre>
			<p>ASP.NET Boilerplate is designed <strong>
			<a href="Module-System.html">modularity</a></strong> in mind. Different modules can 
configure ASP.NET Boilerplate. For example, different modules can add navigation 
provider to add their own menu items to the main menu. (See
				<a href="/Pages/Documents/Localization">localization</a> and
				<a href="/Pages/Documents/Navigation">navigation</a> documents for details 
			on configuring them).</p>
			<h4>Replacing Built-In Services</h4>
			<p><strong>Configuration.ReplaceService</strong> method can be used 
			to <strong>override</strong> a built-in service. For example, you can replace 
			IAbpSession service with your custom implementation as shown below:</p>
			<pre lang="cs"><strong>Configuration.ReplaceService</strong>&lt;IAbpSession, MySession&gt;(DependencyLifeStyle.Transient);</pre>
<p>ReplaceService method has an overload to pass an <strong>action</strong> to 
make replacement in a custom way (you can directly use Castle Windsor for 
advanced registration API).</p>
			<p>Same service can be replaced multiple times (especially, in 
			different modules). Last replaced will be valid (As you know, module 
			PreInitialize methods are executed by <a href="Module-System.html">
			dependency order</a>).</p>
			<h3 id="DocConfigureModules">Configuring Modules</h3>

			<p>Beside framework's own startup configuration, a module can extend 
				<strong>IAbpModuleConfigurations</strong> interface to provide configuration points for the module. 
Example:</p>
			<pre>...
using Abp.Web.Configuration;
...
public override void PreInitialize() 
{
    Configuration.Modules.AbpWebCommon().SendAllExceptionsToClients = true;
}
...</pre>
			<p>In this example, we configured AbpWebCommon module to send all exceptions to 
clients.</p>
			<p>Not every module should define this type of configuration. It's generally needed when a 
module will be re-usable in different applications and needs to be configured on 
startup.</p>
			<h3 id="DocCreateModuleConfig">Creating Configuration For a Module</h3>
			<p>Assume that we have a module named MyModule and it has some configuration 
properties. First, we create a class for these cofigurable properties:</p>
			<pre lang="cs">public class MyModuleConfig
{
    public bool SampleConfig1 { get; set; }

    public string SampleConfig2 { get; set; }
}</pre>
			<p>Then we register this class to 
			<a href="Dependency-Injection.html">Dependency Injection</a> on 
			<strong>PreInitialize</strong> event of MyModule (Thus, 
it will be injectable):</p>
			<pre lang="cs">IocManager.Register&lt;MyModuleConfig&gt;();</pre>
			<p>It should be registered as <strong>Singleton</strong> as in this sample. 
			Now, we can use the following code to configure MyModule in our 
			module's PreInitialize method:</p>
			<pre lang="cs">Configuration.<strong>Get&lt;MyModuleConfig&gt;()</strong>.SampleConfig1 = false;</pre>
			<p>While we can use IAbpStartupConfiguration.Get method as shown 
			below, we can create an extension method to IModuleConfigurations 
			like that:</p>
			<pre lang="cs">public static class MyModuleConfigurationExtensions
{
    <strong>public static MyModuleConfig MyModule(this IModuleConfigurations moduleConfigurations)</strong>
    {
        return moduleConfigurations.AbpConfiguration.Get&lt;MyModuleConfig&gt;();
    }
}</pre>
			<p>Now, other modules can configure this module using the extension 
			method:</p>
			<pre lang="cs">Configuration.Modules.MyModule().SampleConfig1 = false;
Configuration.Modules.MyModule().SampleConfig2 = &quot;test&quot;;</pre>
			<p>This makes easy to investigate module configurations and collect 
			them in a single place (Configuration.Modules...). ABP itself 
			defines extension methods for it's own module configurations.</p>
			<p>At some point, MyModule needs to this configuration. You can inject 
MyModuleConfig and use configured values. Example:</p>
			<pre lang="cs">public class MyService : ITransientDependency
{
    private readonly MyModuleConfig _configuration;

    public MyService(MyModuleConfig configuration)
    {
        _configuration = configuration;
    }

    public void DoIt()
    {
        if (_configuration.SampleConfig2 == &quot;test&quot;)
        {
            //...
        }
    }
}</pre>

			<p>Thus, modules can create central configuration points in ASP.NET Boilerplate 
system.</p>
		</div>
	</body>

</html>
